package mapViewer;


import util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

import context.*;

public class AccountRecord implements Debug.Constants
{ //private static final boolean DEBUG=true; // Overriding Debug.Constants.DEBUG
  
  // MapServer Account:
  private String host;
  private int port;
  private String serviceName;
  private String userID;
  private String password;
  private String resource;

  
  public AccountRecord(String host,int port,String serviceName)
  { this.host=host;
    this.port=port;
    this.serviceName=serviceName;
    this.userID="new user";
    this.password="password";
	this.resource="servlet/FlexiMap";
  }
  
  public AccountRecord(RecordStore rs,int recordId) throws IOException,RecordStoreException
  { readRecord(rs,recordId); }


  public String getAccountName() { return(userID+'@'+serviceName); }

  public void setHost(String host) { this.host=host; }
  public void setPort(int port) { this.port=port; }
  public void setServiceName(String serviceName) { this.serviceName=serviceName; }
  public void setUserID(String userID) { this.userID=userID; }
  public void setPassword(String password) { this.password=password; }
  public void setResource(String resource) { this.resource=resource; }
  
  public String getHost() { return(host); }
  public int getPort() { return(port); }
  public String getServiceName() { return(serviceName); }
  public String getResource() { return(resource); }
  
  public String getUserID() { return(userID); }
  private String getUserID(String authorization)
  {
    if(DEBUG) Debug.println("AccountRecordStore.getUserID: authorization="+authorization);
    String decoded=new String(Base64.decode(authorization.getBytes()));
    if(DEBUG) Debug.println("AccountRecordStore.getUserID: decoded="+decoded);
    int i=decoded.indexOf(':');
    String userID="";
    if(i>0) userID=decoded.substring(0,i);
    if(DEBUG) Debug.println("AccountRecordStore.getUserID: userID="+userID);
    return(userID);
  }

  public String getPassword() { return(password); }
  private String getPassword(String authorization)
  {
    if(DEBUG) Debug.println("AccountRecordStore.getUserID: authorization="+authorization);
    String decoded=new String(Base64.decode(authorization.getBytes()));
    if(DEBUG) Debug.println("AccountRecordStore.getUserID: decoded="+decoded);
    int i=decoded.indexOf(':');
    String password="";
    if(i>0) password=decoded.substring(i+1);
    if(DEBUG) Debug.println("AccountRecordStore.getUserID: password="+password);
    return(password);
  }
  public String getAuthorization()
  { String authorization=new String(Base64.encode((userID+':'+password).getBytes()));
	return(authorization);
  }

  
  public void writeRecord(RecordStore rs,int recordId) throws IOException,RecordStoreException
  { ByteArrayOutputStream bout=new ByteArrayOutputStream();
    DataOutputStream dout=new DataOutputStream(bout);
    if(DEBUG) Debug.println("WRITE["+recordId+"]: "+this);
//    dout.writeUTF(nickName);
    dout.writeUTF(getAuthorization());
    dout.writeUTF(host);
    dout.writeInt(port);
    dout.writeUTF(serviceName);
    dout.writeUTF(resource);
    byte[] record=bout.toByteArray();
    int n=rs.addRecord(record, 0, record.length);
    if(DEBUG) Debug.println("WRITTEN["+n+"]: "+this);
    dout.close();
    bout.close();
  }

  public void readRecord(RecordStore rs,int recordId) throws IOException,RecordStoreException
  { int n=rs.getRecordSize(recordId);
    byte[] rsData=new byte[n];
    ByteArrayInputStream bin = new ByteArrayInputStream(rsData);
    DataInputStream din = new DataInputStream(bin);
    rs.getRecord(recordId,rsData,0);
    String authorization=din.readUTF();
    userID=getUserID(authorization);
    password=getPassword(authorization);
    host=din.readUTF();
    port=din.readInt();
    serviceName=din.readUTF();
    resource=din.readUTF();
    bin.close();
    din.close();
    if(DEBUG) Debug.println("READ["+recordId+"]: "+this);
  }

  public String toString()
  { if(DEBUG) return("AccountRecord[authorization="+getAuthorization()
		  +",host="+host
		  +",port="+port
		  +",serviceName="+serviceName
		  +",resource="+resource
		  +']');
    else return(super.toString());
  }

}
